package game.tycoon.sosial;
import java.util.Random;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.Log;

public class People {
	/*
	 * To change this template, choose Tools | Templates
	 * and open the template in the editor.
	 */
	        int mass;
	        public static final int diameter=30;
	        //coordinates and value of increment
	        public double x, y, xinc, yinc, coll_x, coll_y, coll_xinc, coll_yinc;
	        // x adalah titik pojok kiri gundu
	        // y adalah titik pojok kanan gundu
	        boolean collide;
	        // atribut penanda player
	        private int player;
	        int color;
	        private Bitmap bitmap;
	        private Point LayerSize = new Point(0,0);
	        private boolean myOrderDone = false;
	        //counter animasi
	        int canim = 0;  
	        int ctime = 0;
	        private Bitmap animSurface;
	        private int timeBetweenAnim = 10;//waktu untuk pindah ke frame berikutnya, semakin kecil semakin cepat berubah
	    	
			public int placesToGo[][]; // penanda tempat-tempat yang akan atau telah dikunjungi

			//counter animasi marker(penanda gundu ini yang lagi jalan)
	        boolean marked = false;
	        int cmarked = 0;//counter animasi marker gundu
	        int ctimemarked = 0;
	        private Bitmap animMark;
	        private int timeBetweenAnimMark = 3;//waktu untuk pindah ke frame berikutnya
	        
	        //the constructor
	        public People(Bitmap b, int p, int m, int x, int y, double xinc, double yinc, int c, int width, int height)
	        {
		    	placesToGo = new int[4][2];
		    	placesToGo[0][0] = 0; // indeks 0 untuk status sudah dikunjungi atau belum
		    	placesToGo[0][1] = 0; // indeks 1 untuk lokasi yang harus dikunjungi si pembeli
		    	placesToGo[1][0] = 0;
		    	placesToGo[1][1] = 0;
		    	placesToGo[2][0] = 0;
		    	placesToGo[2][1] = 0;
		    	placesToGo[3][0] = 0;
		    	placesToGo[3][1] = 0;
	        	this.LayerSize.x = width;
	        	this.LayerSize.y = height;
	        	this.player = p;
	        	this.mass = m;
	        	this.x=x;
	        	this.y=y;
	        	this.xinc=xinc;
	        	this.yinc=yinc;
	        	this.color=c;
	        	this.bitmap = b;
	        }	
	    	public Bitmap getBitmap() {
	    		return bitmap;		
	    	}
	    	public void setBitmap(Bitmap bitmap) {
	    		this.bitmap = bitmap;
	    	}
	    	public int isPlayer() {
	    		return player;		
	    	}
	    	public void setPlayer(int _p) {
	    		this.player = _p;
	    	}
	    	public boolean isStop() {
	    		if ((this.xinc==0)&&(this.yinc==0)){
	    			return true;
	    		}
	    		else return false;
	    	}
	        public double getCenterX() {return x+diameter/2;}
	        public double getCenterY() {return y+diameter/2;}
	        public void shoot(double force_x, double force_y, double fs){
	            double acc_x;
	            double acc_y;
	            acc_x = (force_x - fs)/mass;
	            acc_y = (force_y - fs)/mass;
	            xinc = xinc + acc_x;
	            yinc = yinc + acc_y;
	        }
		    public boolean moveTo(int placesId){
		    	boolean patokan = false;
		    	boolean hasil = false;
		    	int patokan_x;
		    	int patokan_y;
//				Log.d("TAG","Kordinat sebelumnya= "+_x+","+_y);
//		    	int X = (40*(_x/40) - (_x%40)) + 10;
//		    	int Y = (80*(_y/80) - (_y%80)) + 10;
//		    	Log.d("TAG","Kordinat = "+X+","+Y);
//		    	if (batasGerak[(int)X][(int)Y]==0){
		    	switch(placesId){
		    	case 0:
		    		patokan_x=0;
		    		patokan_y = 0;
	    			Log.d("PLACES", "Aku Pulang");		    		
		    		break;
		    	case 1:
		    		patokan_x=1*(LayerSize.x/4);
		    		patokan_y = 1*(LayerSize.y/2);
	    			Log.d("PLACES", "Ini Tempat 1");
	    			break;
		    	case 2:
		    		patokan_x = 3*(LayerSize.x/4);
	    			patokan_y = 1*(LayerSize.y/2);	    			    		
	    			Log.d("PLACES", "Ini Tempat 2");
		    		break;
		    	case 3:
		    		patokan_x = 1*(LayerSize.x/2);
		    		patokan_y = 1*(LayerSize.y/4);	    			    		
	    			Log.d("PLACES", "Ini Tempat 3");
		    		break;
		    	case 4:
		    		patokan_x = (LayerSize.x/2);
		    		patokan_y = 3*(LayerSize.y/4);	    			    		
	    			Log.d("PLACES", "Ini Tempat 4");
		    		break;
		    	case 5:
		    		patokan_x = 1*(LayerSize.x/4);
		    		patokan_y = 1*(LayerSize.y/4);	    			    		
	    			Log.d("PLACES", "Ini Tempat 5");
		    		break;
		    	case 6:
		    		patokan_x = 3*(LayerSize.x/4);
		    		patokan_y = 1*(LayerSize.y/4);	    			    		
	    			Log.d("PLACES", "Ini Tempat 6");
		    		break;
		    	default:
		    		patokan_x = 1*(LayerSize.x/4);
		    		patokan_y = 3*(LayerSize.y/4);	    			    		
	    			Log.d("PLACES", "Ini Tempat 7");
		    		break;
		    	}
		    	if (x<patokan_x){
		    		x+=10;
		    		if (x>=patokan_x){
		    			patokan = true;
		    		}
		    	}
		    	else{
		    		x-=10;
		    		if (x<=patokan_x){
		    			patokan = true;
		    		}
		    	}
		    	if (patokan){
		    		if (y<patokan_y){
			    		y+=10;		    			
			    		if (y>=patokan_y){
			    			hasil = true;
			    		}
		    		}
		    		else{
		    			y-=10;
		    			if (y<=patokan_y){
		    				hasil = true;
		    			}
		    		}
		    	}
		    	return hasil;
		    }
		    public boolean doneOrder(){
		    	if (myOrderDone){
		    		return true;
		    	}
		    	else{
		    		return false;
		    	}
		    }
		    public void moveByOrder(){
//		    	try {
//					Thread.sleep(100);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
		    	if (placesToGo[0][1]==0){
		    		Random randomGenerator = new Random();
		    		int randomInt = randomGenerator.nextInt(4);
		    	    Log.d("Random","Generated : " + randomInt);
		    	    //placesToGo[0][0] = 1;
		    	    placesToGo[0][1] = (randomInt+1);
		    	    Log.d("Random","Random tempat pertama");
		    	}
		    	if ((placesToGo[0][0]==1)&&(placesToGo[1][1]==0)){
		    		Random randomGenerator = new Random();
		    		int randomInt = randomGenerator.nextInt(4);
		    	    Log.d("Random","Generated : " + randomInt);
		    		while ((randomInt+1)==placesToGo[0][1]){
			    		randomInt = randomGenerator.nextInt(4);
			    	    Log.d("Random","Generated : " + randomInt);	    			
		    		}
		    	    placesToGo[1][1] = (randomInt+1);
		    	    Log.d("Random","sampe di tempat kedua");
		    	}
		    	if ((placesToGo[1][0]==1)&&(placesToGo[2][1]==0)){
		    		Random randomGenerator = new Random();
		    		int randomInt = randomGenerator.nextInt(4);
		    	    Log.d("Random","Generated : " + randomInt);
		    		while (((randomInt+1)==placesToGo[0][1])||((randomInt+1)==placesToGo[1][1])){
			    		randomInt = randomGenerator.nextInt(4);
			    	    Log.d("Random","Generated : " + randomInt);	    			
		    		}
		    		placesToGo[2][1] = (randomInt+1);
		    	    Log.d("Random","sampe di tempat ketiga");
		    		
		    	}
		    	if ((placesToGo[2][0]==1)&&(placesToGo[3][1]==0)){
		    		Random randomGenerator = new Random();
		    		int randomInt = randomGenerator.nextInt(3);
		    	    Log.d("Random","Generated : " + randomInt);
		    	    Log.d("Random","sampe di tempat keempat");
		    		placesToGo[3][1] = (randomInt+4);
		    	}
		    	if (placesToGo[0][0]==0){
		    		if (moveTo(placesToGo[0][1])){
		    			placesToGo[0][0]=1;
		    		}
		    	}
		    	else if (placesToGo[1][0]==0){
		    		if (moveTo(placesToGo[1][1])){
		    			placesToGo[1][0]=1;
		    		}
		    		
		    	}
		    	else if (placesToGo[2][0]==0){
		    		if (moveTo(placesToGo[2][1])){
		    			placesToGo[2][0]=1;
		    		}
		    		
		    	} else if (placesToGo[3][0]==0){
		    		if (moveTo(placesToGo[3][1])){
		    			placesToGo[3][0]=1;
		    			Log.d("POlaces", "Pekerjaan selesai");
		    			
		    		}
		    		
		    	}
		    	else if (placesToGo[3][0]==1){
		    		if (moveTo(0)){
			    		myOrderDone = true;
		    			Log.d("POlaces", "Pulang dah");		    			
		    		}
		    	}
		    	
		    }
	        
	        public void move(int fr){        	
	        	double acc_x=0;
	            double acc_y=0;            
	                x+=xinc;
	                y+=yinc;                      
	                animateEffect(fr,xinc,yinc);
	                if (xinc>0){
	                    acc_x = -(fr*1.0/mass);
	                    xinc = xinc + acc_x;
	                    if (xinc<0){
	                        xinc=0;
	                    }
	                }
	                else if (xinc<0){
	                    acc_x = -(fr*1.0/mass);
	                    xinc = xinc - acc_x;
	                    if (xinc>0){
	                        xinc=0;
	                    }
	                }
	                if (yinc>0){
	                    acc_y = -(fr*1.0/mass);
	                    yinc = yinc + acc_y;
	                    if (yinc<0){
	                        yinc=0;
	                    }
	                }
	                else if (yinc<0){
	                    acc_y = -(fr*1.0/mass);
	                    yinc = yinc - acc_y;
	                    if (yinc>0){
	                        yinc=0;
	                    }
	                }
	                //when the ball bumps against a boundary, it bounces off
	                if(x<0){
//	                    xinc=-xinc;
	                    x=45;
	                	xinc=0;
	                	yinc=0;
	                    
	                }
	                if(x>LayerSize.x-diameter)
	                {
	                	
//	                    xinc=-xinc;
	                    x = LayerSize.x-diameter-45;
	                	xinc=0;
	                	yinc=0;
	                    
	                }

	                if(y<0){
//	                    yinc=-yinc;
	                    y=45;
	                	xinc=0;
	                	yinc=0;
	                }
	                if(y>LayerSize.y-diameter)
	                {
//	                    yinc=-yinc;
	                    y = LayerSize.y-diameter-45;
	                	xinc=0;
	                	yinc=0;
	                }               
	        }	

	        private void animateEffect(int fr,double xinc2, double yinc2) {        	
	        	if(!isStop()) {//jika tidak sedang berhenti        		
	        		switch(fr) {
	        		case PeopleController.TANAH_BIASA :
	        			//di tanah biasa tidak ada cipratan
	        			animSurface = null;
	        			break;
	        		case PeopleController.AIR :
	        			animSurface = PeopleController.animwater[canim];
	        			break;
	        		case PeopleController.BATU :
	        			animSurface = PeopleController.animStone[canim];
	        			break;
	        		case PeopleController.RUMPUT:
	        			animSurface = PeopleController.animGrass[canim];
	        			break;
	        		case PeopleController.LUMPUR :
	        			animSurface = PeopleController.animMud[canim];
	        			break;
	        		}        	
	        		ctime++;//waktu berikutnya
	        		if(ctime==timeBetweenAnim) {
	        			ctime = 0;
	        			canim = (canim + 1) % 5;
	        		}
	        	}else{//jika berhenti
	        		animSurface = null;
	        	}
	        }
	        
	        private void drawMarker(Canvas canvas) {
	        	animMark = PeopleController.animMarker[cmarked];
	        	canvas.drawBitmap(animMark,(int) (x + (bitmap.getWidth()*1.0/2) - animMark.getWidth()/2),(int) (y + (bitmap.getHeight()/2) - animMark.getHeight()/2), null);
	        	ctimemarked++;//waktu berikutnya
	        	if(ctimemarked==timeBetweenAnimMark) {
	        		ctimemarked = 0;
	        		cmarked = (cmarked+1) % 14;
	        	}
	        }
	        
	        public void hit(People b)
	        {
	        	 if(!collide)
	             {   
	        		 x += xinc;
	        		 y += yinc;
	             }
	        }
	        public void draw(Canvas canvas) {        	
	    		//gambar gundu
	        	canvas.drawBitmap(bitmap,(int)x,(int)y, null);
	    		//gambar animasi di atas gundu
//	        	if(animSurface!=null) {
//	        		canvas.drawBitmap(animSurface, (int)(x-(animSurface.getWidth() - bitmap.getWidth())/2), (int)(y - (animSurface.getHeight() - bitmap.getHeight())/2),null);
//	        	}
//	        	if(isStop() && marked) {//jika gundu berhenti dan marked
//	        		drawMarker(canvas);
//	        	}
	        }
	        
	        public void drawScaledPeople(Canvas canvas,float size) {
	        	float left = (float)(this.getCenterX() - size/2);
	        	float top =  (float)(this.getCenterY() - size/2);
	        	canvas.drawBitmap(bitmap, null, new RectF(left,top,left+size,top+size), null);
	        }

}
